package com.ruoyi.member.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.member.domain.Member;
import com.ruoyi.member.domain.MemberLevel;
import com.ruoyi.member.mapper.MemberMapper;
import com.ruoyi.member.service.IMemberLevelService;
import com.ruoyi.member.service.IMemberService;

/**
 * 会员管理Service业务层处理
 * 
 * @author ruoyi
 */
@Service
public class MemberServiceImpl implements IMemberService {
    @Autowired
    private MemberMapper memberMapper;

    @Autowired
    private IMemberLevelService memberLevelService;

    /**
     * 查询会员列表
     * 
     * @param member 会员信息
     * @return 会员集合
     */
    @Override
    public List<Member> selectMemberList(Member member) {
        return memberMapper.selectMemberList(member);
    }

    /**
     * 通过会员ID查询会员
     * 
     * @param memberId 会员ID
     * @return 会员对象信息
     */
    @Override
    public Member selectMemberById(Long memberId) {
        return memberMapper.selectMemberById(memberId);
    }

    /**
     * 通过用户名查询会员
     * 
     * @param username 用户名
     * @return 会员对象信息
     */
    @Override
    public Member selectMemberByUsername(String username) {
        return memberMapper.selectMemberByUsername(username);
    }

    /**
     * 通过手机号查询会员
     * 
     * @param mobile 手机号
     * @return 会员对象信息
     */
    @Override
    public Member selectMemberByMobile(String mobile) {
        return memberMapper.selectMemberByMobile(mobile);
    }

    /**
     * 注册会员信息
     * 
     * @param member 会员信息
     * @return 结果
     */
    @Override
    @Transactional
    public int registerMember(Member member) {
        member.setLevelId(1); // 默认等级
        member.setPoints(0); // 初始积分
        member.setStatus("0"); // 正常状态
        member.setDelFlag("0"); // 未删除
        member.setPassword(SecurityUtils.encryptPassword(member.getPassword())); // 密码加密
        return memberMapper.insertMember(member);
    }

    /**
     * 修改会员
     * 
     * @param member 会员信息
     * @return 结果
     */
    @Override
    public int updateMember(Member member) {
        return memberMapper.updateMember(member);
    }

    /**
     * 修改会员状态
     * 
     * @param member 会员信息
     * @return 结果
     */
    @Override
    public int updateMemberStatus(Member member) {
        return memberMapper.updateMember(member);
    }

    /**
     * 修改会员积分
     * 
     * @param member 会员信息
     * @return 结果
     */
    @Override
    @Transactional
    public int updateMemberPoints(Member member) {
        // 根据积分获取对应等级
        MemberLevel level = memberLevelService.selectMemberLevelByPoints(member.getPoints());
        if (level != null) {
            member.setLevelId(level.getLevelId());
        }
        return memberMapper.updateMemberPoints(member);
    }

    /**
     * 重置会员密码
     * 
     * @param member 会员信息
     * @return 结果
     */
    @Override
    public int resetMemberPwd(Member member) {
        member.setPassword(SecurityUtils.encryptPassword(member.getPassword()));
        return updateMember(member);
    }

    /**
     * 校验用户名称是否唯一
     * 
     * @param username 用户名
     * @return 结果
     */
    @Override
    public boolean checkUsernameUnique(String username) {
        return memberMapper.checkUsernameUnique(username) == 0;
    }

    /**
     * 校验手机号码是否唯一
     *
     * @param member 会员信息
     * @return 结果
     */
    @Override
    public boolean checkMobileUnique(Member member) {
        Long memberId = StringUtils.isNull(member.getMemberId()) ? -1L : member.getMemberId();
        Member info = memberMapper.selectMemberByMobile(member.getMobile());
        if (StringUtils.isNotNull(info) && info.getMemberId().longValue() != memberId.longValue()) {
            return false;
        }
        return true;
    }

    /**
     * 删除会员信息
     * 
     * @param memberId 会员ID
     * @return 结果
     */
    @Override
    public int deleteMemberById(Long memberId) {
        return memberMapper.deleteMemberById(memberId);
    }

    /**
     * 批量删除会员信息
     * 
     * @param memberIds 需要删除的会员ID
     * @return 结果
     */
    @Override
    public int deleteMemberByIds(Long[] memberIds) {
        return memberMapper.deleteMemberByIds(memberIds);
    }
}